package com.hc.cesium.controller;

import com.hc.cesium.mapper.LampMapper;
import com.hc.cesium.model.Lamp;
import com.hc.cesium.model.User;
import com.hc.cesium.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class LampController {
    @Autowired
    private LampMapper lampMapper;
    //查询所有灯杆
    @CrossOrigin
    @RequestMapping(value = "/lamp",method = RequestMethod.GET)
    @ResponseBody
    public Result<List<Lamp>> test(){
        Lamp lamp = new Lamp();
        List<Lamp> lamps = lampMapper.findAll(lamp);
        return Result.success(lamps);
    }
    //添加灯杆
    @RequestMapping(value = "/saveLamp",method = RequestMethod.GET)
    @ResponseBody
    public Result<String> saveLamp(Lamp lamp){
        System.out.println(lamp);
        int save = lampMapper.save(lamp);
        if (save==1){
            return Result.success("添加成功");
        }
        return Result.success("添加失败");
    }
    //通过灯杆id查询
    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    @ResponseBody
    public Result<Lamp> findById(@RequestParam(value = "card") String  card){
        Lamp lamp = lampMapper.findById(card);
        System.out.println(lamp);
        if (lamp!=null){
            return Result.success(lamp);
        }
        return null;
    }
    @RequestMapping(value = "/findByIdR/{card}",method = RequestMethod.GET)
    @ResponseBody
    public Result<Lamp> findByIdR(@PathVariable(value = "card") String  card){
        Lamp lamp = lampMapper.findById(card);
        System.out.println(lamp);
        if (lamp!=null){
            return Result.success(lamp);
        }
        return null;
    }
    //通过灯杆id 删除
    @RequestMapping(value = "/delById",method = RequestMethod.GET)
    @ResponseBody
    public Result<String> delById(@RequestParam(value = "card") String card){
        int del = lampMapper.delById(card);
        if (del==1){
          return Result.success("删除成功");
        }
        return Result.success("删除失败");
    }
    //对灯杆进行修改
    @RequestMapping(value = "/update",method = RequestMethod.GET)
    @ResponseBody
    public Result<String> update(Lamp lamp){
        System.out.println(lamp);
        int update = lampMapper.update(lamp);

        if (update==1){
            return Result.success("修改成功");
        }
        return Result.success("修改失败");
    }
}
